package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.UserSubscriptionTemplateJdbc;
import com.wuseguang.springcloud.dao.repository.UserSubscriptionTemplateRepository;
import com.wuseguang.springcloud.entity.UserSubscriptionTemplate;
import com.wuseguang.springcloud.entity.WeChatTemplate;
import com.wuseguang.springcloud.service.UserSubscriptionTemplateService;
import com.wuseguang.springcloud.util.Result;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户订阅的模板信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-01
 */
@Service
public class UserSubscriptionTemplateServiceImpl  implements UserSubscriptionTemplateService {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserSubscriptionTemplateJdbc userSubscriptionTemplateJdbc;
    @Autowired
    private UserSubscriptionTemplateRepository userSubscriptionTemplateRepository;

    @Override
    public Result findUserTemplateClick(String userId,Integer type) {

        logger.info("查询用户未订阅模板");
        //查询用户的openID
        String openIdSql = "select open_id_one,open_id_two from parent  where id = ?";
        Map<String,Object> openIdMap = userSubscriptionTemplateJdbc.queryMap(openIdSql,userId);


        //查询家长没有订阅的模板
       String sql = "select w.id,w.template_id  from we_chat_template w " +
               " WHERE not exists (select * from user_subscription_template ust" +
               " where  w.id = ust.template_id AND ust.open_id =? AND ust.state = 0  ) " +
               " and w.version_type  =? and  w.school_id =0  limit 3 ";

        List<Map<String,Object>> list = userSubscriptionTemplateJdbc.queryListMap(sql,openIdMap.get("open_id_one"),type);

        return new Result(list);
    }

    @Override
    public Result addUserTemplate(String userId, Integer[] id) {


        //查询用户的openID
        String openIdSql = "select open_id_one,open_id_two from parent  where id = ?";
        Map<String,Object> openIdMap = userSubscriptionTemplateJdbc.queryMap(openIdSql,userId);

        logger.info("添加用的订阅模板");
        //循环数据库模板id
        for (int i = 0;i<id.length;i++){
            UserSubscriptionTemplate u = new UserSubscriptionTemplate();
            u.setUserId(Integer.valueOf(userId));
            u.setTemplateId(id[i]);
            u.setState(0);
            u.setOpenId(openIdMap.get("open_id_one").toString());
            userSubscriptionTemplateRepository.save(u);
        }
        return new Result();
    }

    @Override
    public Result deleteUserTemplate(String userId, String[] templateId) {

        //查询用户的openID
        String openIdSql = "select open_id_one,open_id_two from parent  where id = ?";
        Map<String,Object> openIdMap = userSubscriptionTemplateJdbc.queryMap(openIdSql,userId);
        for (int i = 0;i<templateId.length;i++){
           String sql = "UPDATE user_subscription_template ust " +
                   " LEFT JOIN we_chat_template wt ON ( wt.id = ust.template_id ) " +
                   " SET ust.`state` = 1 WHERE  wt.template_id = ?  AND ust.open_id =?";
            userSubscriptionTemplateJdbc.updateByParameter(sql,templateId[i],openIdMap.get("open_id_one"));
        }
        return new Result();
    }
}
